<template>
  <div style="margin-top:30px;">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">

      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="所属机构" prop="partyGroupCode" style="margin-right:15px">
              <partyGroupSelect ref="partyGroupSelect" @setPartyGroupCode="setPartyGroupCode" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="部门名称" prop="name" style="margin-right:15px">
              <el-input v-model="form.name" placeholder="请输入部门名称" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit" :disabled="isDisable">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import request from '@/utils/request'
import partyGroupSelect from '@/views/components/partyGroupSelect.vue'
Vue.use(VXETable)
export default {
  name: 'DepartmentEdit',
  components: {
    partyGroupSelect
  },
  data() {
    return {
      isDisable:false,

      emptyForm: {},
      form: {
        code: '',
        partyGroupCode: '',
        name: ''
      },
      rules: {
           partyGroupCode: [
          { required: true, message: '请选择所属机构', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 保存空表单对象
    this.emptyForm = JSON.parse(JSON.stringify(this.form))
  },
  methods: {
    // 选择组织机构
    setPartyGroupCode(value) {
      this.form.partyGroupCode = value
    },
    // 获取页面信息
    getInfo(code) {
      // 查询到数据之前先展示空表单
      this.form = JSON.parse(JSON.stringify(this.emptyForm))
      request({ url: `/arpa-basic-api/department/${code}`, method: 'get' }).then(response => {
        this.form = response.data
        this.$refs.partyGroupSelect.setSelectValue(this.form.partyGroupCode)
      })
    },
    // 提交
    funcSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.isDisable = true

          const rLoading = this.openLoading();
          const data = this.form
          request({ url: '/arpa-basic-api/department', method: 'put', params: data }).then(response => {
            rLoading.close();
            this.$message({ message: response.msg, type: 'success' })
            this.$emit('fatherEditMethod')
            this.isDisable = false
 
          }).then(() => {
              this.$refs['form'].resetFields()
              this.$refs.partyGroupSelect.setSelectValue('')
            }).catch(() => this.isDisable = false)
        } else {
          return false
        }
      })
    },
    // 取消
    funcCancel() {
      this.$emit('fatherCancelMethod', 'edit')
    }

  }
}
</script>
<style lang="scss" scoped>
.dialog-footer {
    margin:15px 0 15px 0;
    width: 175px;
    float: right;
}
.el-select {
  width: 100%;
}
</style>
